/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-20 11:19:02
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-04-28 16:19:46
 * @FilePath: \cxl-driving-training\src\views\drivingSchoolManagement\drivingSchool\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import router from '@/router'
import { drivingSchoolDisable, drivingSchoolList, drivingSchoolListInterface } from '@/apis/drivingSchool'
import { DICT_DATA } from '@/utils/dict-data'
import { ElMessage } from 'element-plus'
import { codeToText } from 'element-china-area-data'
import { userStore } from '@/store/user.store'

export default class CompanyAdminData extends BaseData implements BaseMethod {
    searchData = { fullName: '', shortName: '', disable: '-1' }
    uStore = userStore()
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        const params: drivingSchoolListInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }

        if (this.searchData.fullName) {
            params.fullName = this.searchData.fullName
        }
        if (this.searchData.shortName) {
            params.shortName = this.searchData.shortName
        }
        if (this.searchData.disable !== '' && this.searchData.disable !== '-1') {
            // params.disable = Number(this.searchData.disable)
        }

        drivingSchoolList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total

                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false
                    // element.city = codeToText[element.city.split('/')[1]]
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = { fullName: '', shortName: '', disable: '-1' }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type)
        if (type === 'create') {
            router.push({ path: '/driving-school/operation', query: { type } })
        } else if (type === 'edit' || type === 'look') {
            router.push({ path: '/driving-school/operation', query: { type, id: data.id } })
        } else if (type === 'switch') {
            // drivingSchoolDisable(data.status ? 0 : 1, data.id).then(res => {
            //     if (Number(res.data.code) === 200) {
            //         ElMessage.success('修改成功')
            //         // this.getData()
            //         this.uStore.updateDrivingSchoolSimple()
            //     }
            // })
        }
    }
}
